home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 February: Tool Chest / Dev.CD Feb 95 / Dev.CD Feb 95.toast / Tool Chest / Development Tools & Languages / Dylan Related / Mindy-1.1 (sources only) / mindy-1.1 / comp / lexer.l < prev    next >
Encoding:
Text File  |  1994-07-13  |  4.6 KB  |  232 lines  |  [TEXT/ttxt]

  1. /**********************************************************************\
  2. *
  3. *  Copyright (c) 1994  Carnegie Mellon University
  4. *  All rights reserved.
  5. *  
  6. *  Use and copying of this software and preparation of derivative
  7. *  works based on this software are permitted, including commercial
  8. *  use, provided that the following conditions are observed:
  9. *  
  10. *  1. This copyright notice must be retained in full on any copies
  11. *     and on appropriate parts of any derivative works.
  12. *  2. Documentation (paper or online) accompanying any system that
  13. *     incorporates this software, or any part of it, must acknowledge
  14. *     the contribution of the Gwydion Project at Carnegie Mellon
  15. *     University.
  16. *  
  17. *  This software is made available "as is".  Neither the authors nor
  18. *  Carnegie Mellon University make any warranty about the software,
  19. *  its performance, or its conformity to any specification.
  20. *  
  21. *  Bug reports, questions, comments, and suggestions should be sent by
  22. *  E-mail to the Internet address "gwydion-bugs@cs.cmu.edu".
  23. *
  24. ***********************************************************************
  25. *
  26. * $Header: lexer.l,v 1.11 94/07/12 20:45:41 wlott Exp $
  27. *
  28. * This file is the lexical analizer.
  29. *
  30. \**********************************************************************/
  31.  
  32. %{
  33. #include <string.h>
  34.  
  35. #include "mindycomp.h"
  36. #include "lexer.h"
  37. #include "src.h"
  38. #include "parser.tab.h"
  39.  
  40. int line_count = 1;
  41.  
  42. #define is(type) \
  43.     do { yylval.token = make_token(yytext, yyleng); return type; } while(0)
  44.  
  45. static void skip_multi_line_comment(void);
  46. static struct token *make_token(char *ptr, int len);
  47.  
  48. %}
  49.  
  50. D    [0-9]
  51. E    [esdx][-+]?{D}+
  52.  
  53. A    [a-z]
  54. G    [!&*<=>|^$%@_]
  55. S    [-+~?/]
  56.  
  57. DGS    ({D}|{G}|{S})
  58. ADGS    ({A}|{DGS})
  59.  
  60. N    ((({G}{DGS}*)?{A}{ADGS}*)|({D}{DGS}*({A}{DGS}+)*{A}{A}{ADGS}*))
  61.  
  62. O    ":="|"+"|"-"|"*"|"/"|"="|"=="|"<"|">"|"<="|">="|"~="|"&"|"|"|"^"
  63.  
  64. STR    \"(([ !#-\[\]-~])|(\\["\\abefnrt0]))*\"
  65.  
  66. %%
  67.  
  68. [ \t\f]+    ;
  69. [\n]        line_count++;
  70.  
  71. "//".*        ;
  72. "/*"        skip_multi_line_comment();
  73.  
  74. abstract    is(ABSTRACT);
  75. above        is(ABOVE);
  76. begin        is(DBEGIN);
  77. below        is(BELOW);
  78. block        is(BLOCK);
  79. by        is(BY);
  80. case        is(CASE);
  81. class        is(CLASS);
  82. cleanup        is(CLEANUP);
  83. concrete    is(CONCRETE);
  84. constant    is(CONSTANT);
  85. define        is(DEFINE);
  86. else        is(ELSE);
  87. elseif        is(ELSEIF);
  88. end        is(END);
  89. exception    is(EXCEPTION);
  90. finally        is(FINALLY);
  91. for        is(FOR);
  92. free        is(FREE);
  93. from        is(FROM);
  94. generic        is(GENERIC);
  95. handler        is(HANDLER);
  96. if        is(IF);
  97. in        is(IN);
  98. inherited    is(INHERITED);
  99. instance    is(INSTANCE);
  100. keyed-by    is(KEYED_BY);
  101. keyword        is(KEYWORD_RESERVED_WORD);
  102. let        is(LET);
  103. local        is(LOCAL);
  104. method        is(METHOD);
  105. open        is(OPEN);
  106. otherwise    is(OTHERWISE);
  107. primary        is(PRIMARY);
  108. required    is(REQUIRED);
  109. seal        is(SEAL);
  110. sealed        is(SEALED);
  111. select        is(SELECT);
  112. slot        is(SLOT);
  113. subclass    is(SUBCLASS);
  114. then        is(THEN);
  115. to        is(TO);
  116. unless        is(UNLESS);
  117. until        is(UNTIL);
  118. variable    is(VARIABLE);
  119. virtual        is(VIRTUAL);
  120. while        is(WHILE);
  121.  
  122. module        is(MODULE);
  123. library        is(LIBRARY);
  124. export        is(EXPORT);
  125. create        is(CREATE);
  126. use        is(USE);
  127. all        is(ALL);
  128.  
  129. "prefix:"    is(PREFIX_OPTION);
  130. "import:"    is(IMPORT_OPTION);
  131. "exclude:"    is(EXCLUDE_OPTION);
  132. "export:"    is(EXPORT_OPTION);
  133. "rename:"    is(RENAME_OPTION);
  134.  
  135. "("        is(LPAREN);
  136. ")"        is(RPAREN);
  137. ","        is(COMMA);
  138. "."        is(DOT);
  139. ";"        is(SEMI);
  140. "["        is(LBRACKET);
  141. "]"        is(RBRACKET);
  142. "{"        is(LBRACE);
  143. "}"        is(RBRACE);
  144. "::"        is(COLON_COLON);
  145. "-"        is(MINUS);
  146. "~"        is(TILDE);
  147. "="        is(EQUAL);
  148. "=="        is(EQUAL_EQUAL);
  149. "=>"        is(ARROW);
  150. "#("        is(SHARP_PAREN);
  151. "#["        is(SHARP_BRACKET);
  152. "#t"        is(SHARP_T);
  153. "#f"        is(SHARP_F);
  154. "#next"        is(NEXT);
  155. "#rest"        is(REST);
  156. "#key"        is(KEY);
  157. "#all-keys"    is(ALL_KEYS);
  158.  
  159. [-+]?{D}+    is(INTEGER);
  160. #x[0-9a-f]+    is(INTEGER);
  161. #o[0-7]+    is(INTEGER);
  162. #b[01]+        is(INTEGER);
  163.  
  164. [-+]?{D}*\.{D}+{E}?    is(FLOAT);
  165. [-+]?{D}+\.{D}*{E}?    is(FLOAT);
  166. [-+]?{D}+{E}        is(FLOAT);
  167.  
  168. '[ -&(-\[\]-~]'        is(CHARACTER);
  169. '\\['\\abefnrt0]'    is(CHARACTER);
  170.  
  171. {STR}        is(STRING);
  172.  
  173. {O}        is(BINARY_OPERATOR);
  174. {N}        is(SYMBOL);
  175. \\{O}        is(SYMBOL);
  176. {N}:        is(KEYWORD);
  177. #{STR}        is(SYMBOL_LITERAL);
  178.  
  179. .        is(BOGUS);
  180.  
  181. %%
  182.  
  183. static void skip_multi_line_comment(void)
  184. {
  185.     int depth = 1;
  186.     int c, prev = '\0';
  187.     
  188.     while (1) {
  189.     c = input();
  190.     switch (c) {
  191.       case EOF:
  192.         return;
  193.       case '\n':
  194.         line_count++;
  195.         prev = c;
  196.         break;
  197.       case '/':
  198.         if (prev == '*')
  199.         if (--depth == 0)
  200.             return;
  201.         else
  202.             prev = 0;
  203.         else
  204.         prev = c;
  205.         break;
  206.       case '*':
  207.         if (prev == '/') {
  208.         depth++;
  209.         prev = 0;
  210.         }
  211.         else
  212.         prev = c;
  213.         break;
  214.       default:
  215.         prev = c;
  216.         break;
  217.     }
  218.     }
  219. }
  220.  
  221. static struct token *make_token(char *ptr, int len)
  222. {
  223.     struct token *token = malloc(sizeof(struct token) + len + 1);
  224.  
  225.     token->length = len;
  226.     memcpy(token->chars, ptr, len);
  227.     token->line = line_count;
  228.     token->chars[len] = 0;
  229.  
  230.     return token;
  231. }
  232.